Ext.define('cceaf.control.storage.ProductField',{
	extend: 'Ext.form.TriggerField', 
    alias: 'widget.cceafproductfield',
    requires: [
        'cceaf.control.storage.SelectProductGrid'
    ],
           
	triggerClass : 'x-form-date-trigger',
	
	nameOfProductid: null,
	nameOfProductno: null,
	nameOfProductname: null,
	nameOfHelpcode: null,
	nameOfSpecification: null,
	nameOfUnit: null,
	
	initComponent : function(){
		cceaf.control.storage.ProductField.superclass.initComponent.call(this);

        this.addEvents(
            'select'
        );
    },

    initEvents: function() {
    	cceaf.control.storage.ProductField.superclass.initEvents.call(this);
        this.keyNav = new Ext.KeyNav(this.el, {
            "down": function(e) {
                this.onTriggerClick();
            },
            scope: this,
            forceKeyDown: true
        });
    },
	
    onTriggerClick : function(){
        if(this.disabled){
            return;
        }
        
        this.showSelectWin();
    },
    
    listeners:{
		'focus': function(){
			this.selectText();
		},
		'specialkey': function(obj,e){
			if(e.getKey() == 13){//按Enter
				this.showSelectWin();
			}
			if(e.getKey() == 8){//按Backspace
				this.clearSelectValue();
			}
		}
    },
    
    clearSelectValue: function(){
    	var triggerField = this;
    	var editorGridPane = triggerField.up('cceafdetaileditorgridpanel');
		
    	if(editorGridPane != null){
    		var selectedRow = editorGridPane.getSelectionModel().getLastSelected();
			if(this.nameOfProductid != null && this.nameOfProductid != ""){
				selectedRow.set(this.nameOfProductid,'');
			}
			if(this.nameOfProductno != null && this.nameOfProductno != ""){
				selectedRow.set(this.nameOfProductno,'');
			}
			if(this.nameOfProductname != null && this.nameOfProductname != ""){
				selectedRow.set(this.nameOfProductname,'');
			}
			if(this.nameOfHelpcode != null && this.nameOfHelpcode != ""){
				selectedRow.set(this.nameOfHelpcode,'');
			}
			if(this.nameOfSpecification != null && this.nameOfSpecification != ""){
				selectedRow.set(this.nameOfSpecification,'');
			}
			if(this.nameOfUnit != null && this.nameOfUnit != ""){
				selectedRow.set(this.nameOfUnit,'');
			}
    	} else {
    		var panel = this.up('panel');
        	if(this.nameOfProductid != null && this.nameOfProductid != ""){
    			panel.down('[name=' + this.nameOfProductid + ']').setValue('');
    		}
    		if(this.nameOfProductno != null && this.nameOfProductno != ""){
    			panel.down('[name=' + this.nameOfProductno + ']').setValue('');
    		}
    		if(this.nameOfProductname != null && this.nameOfProductname != ""){
    			panel.down('[name=' + this.nameOfProductname + ']').setValue('');
    		}
    		if(this.nameOfHelpcode != null && this.nameOfHelpcode != ""){
    			panel.down('[name=' + this.nameOfHelpcode + ']').setValue('');
    		}
    		if(this.nameOfSpecification != null && this.nameOfSpecification != ""){
    			panel.down('[name=' + this.nameOfSpecification + ']').setValue('');
    		}
    		if(this.nameOfUnit != null && this.nameOfUnit != ""){
    			panel.down('[name=' + this.nameOfUnit + ']').setValue('');
    		}
    	}
    	
    },
    
    showSelectWin: function(){
    	var selectProductWin = Ext.getCmp("_selectProductWin");
		if(selectProductWin == null){
			selectProductWin = new Ext.Window({
	    		id:'_selectProductWin',
	        	title: '选择产品',
	        	modal: true,
	        	closeAction: 'hide',
	        	//x: this.getPosition()[0],
	        	//y: this.getPosition()[1],
	        	height: 420,
	        	width: 700,
	        	autoScroll: true,
	        	layout: 'fit',
	        	listeners:{
	        		'show': function(){
	        			var grid = this.down('cceafselectproductgrid');
	        			grid.down('[name=q_product_productno]').focus(true,200);
	        		}
	        	},
	        	items: new cceaf.control.storage.SelectProductGrid({
	        		triggerField: this
	        	})
			});
		}
		else{
			//selectProductWin.setPosition(this.getPosition()[0],this.getPosition()[1]);
		}
		selectProductWin.show();
		
		var selectProductGrid = selectProductWin.down('cceafselectproductgrid');
		selectProductGrid.triggerField = this;
    }
});